package com.mayihr.new_learn_fx.UIControl.treeviewsample;
import javafx.application.Application;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class TreeViewSample1 extends Application {

    private final Node rootIcon2 = new ImageView(
            new Image(getClass().getResourceAsStream("folder_16.png"))
    );
    private final Node rootIcon = new ImageView(
            new Image(getClass().getResourceAsStream("folder_16.png"))
    );

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Tree View Sample");

        user user = new user("test", 111);
        TreeItem<user> rootItem = new TreeItem<> ( user,rootIcon2);
        rootItem.setExpanded(true); //设置展开
        for (int i = 1; i < 6; i++) {
            user u=new user("tom"+i,i);
            TreeItem<user> item = new TreeItem<>(u,rootIcon);
            rootItem.getChildren().add(item);
        }


//
//        TreeItem<String> rootItems= new TreeItem<> ("Inbox", rootIcon);
//        rootItems.setExpanded(true);
//        for (int i = 1; i < 6; i++) {
//            TreeItem<String> item = new TreeItem<> ("Message" + i);
//            rootItems.getChildren().add(item);
//        }
//        rootItems.getChildren().add(rootItem);
        TreeView<?> tree = new TreeView<> (rootItem);
        StackPane root = new StackPane();
        root.getChildren().add(tree);
        primaryStage.setScene(new Scene(root, 300, 250));
        primaryStage.show();
    }

    class user{
        private String name;
        private int age;

        public user() {
        }

        public user(String name, int age) {
            this.name = name;
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        @Override
        public String toString() {
            return "user{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }
}